package jp.co.withone.osgi.gadget.pictureviewer.impl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;

/**
 * PictureViewerSettingView class provide setting view.
 * 
 * @author funakky
 */
public class PictureViewerSettingView extends JFrame {

	private final Activator manager;

	/**
	 * Create new setting view.
	 * 
	 * @param manager
	 */
	public PictureViewerSettingView(Activator manager) {
		super(Activator.RB.getString("setting.view.title"));
		this.manager = manager;
		initComponents();
	}

	private void initComponents() {
		final JFrame frame = this;
		this.setBounds(0, 0, 250, 70);

		// create components
		final JLabel intervalLabel = new JLabel(Activator.RB
				.getString("change.picture.interval"));
		final JTextField intervalText = new JTextField(String
				.valueOf(this.manager.getInterval() / 1000L));
		final JLabel intervalUnitLabel = new JLabel(Activator.RB
				.getString("interval.unit"));
		final JButton okButton = new JButton(Activator.RB.getString("ok"));
		final JButton applyButton = new JButton(Activator.RB.getString("apply"));
		final JButton cancelButton = new JButton(Activator.RB
				.getString("cancel"));

		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				final Object source = e.getSource();
				if (source == okButton || source == applyButton) {
					int interval = 0;
					try {
						interval = Integer.parseInt(intervalText.getText());
					} catch (NumberFormatException ex) {
					}
					if (interval < 2 || interval > 3600) {
						JOptionPane.showMessageDialog(frame, Activator.RB
								.getString("interval.warn.message"),
								Activator.RB.getString("interval.warn.title"),
								JOptionPane.ERROR_MESSAGE);
						return;
					}
					manager.setInterval(interval * 1000L);
				}
				if (source == okButton || source == cancelButton) {
					frame.dispose();
				}
			}
		};

		okButton.addActionListener(actionListener);
		applyButton.addActionListener(actionListener);
		cancelButton.addActionListener(actionListener);

		// set layout
		final GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		{
			// set horizontal group
			final ParallelGroup pg1 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg1.addComponent(intervalLabel).addComponent(okButton);
			final ParallelGroup pg2 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg2.addComponent(intervalText).addComponent(applyButton);
			final ParallelGroup pg3 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg3.addComponent(intervalUnitLabel).addComponent(cancelButton);

			layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
					pg1).addGroup(pg2).addGroup(pg3));
		}
		{
			// set vertical group
			final ParallelGroup pg1 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg1.addComponent(intervalLabel).addComponent(intervalText)
					.addComponent(intervalUnitLabel);
			final ParallelGroup pg2 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg2.addComponent(okButton).addComponent(applyButton).addComponent(
					cancelButton);

			layout.setVerticalGroup(layout.createSequentialGroup()
					.addGroup(pg1).addGroup(pg2));
		}

	}
}
